<template>
  <div>
    <i-page-header class="product_tabs" :title="title" />
    <Card :bordered="false" dis-hover class="ivu-mt">
      <div class="search-inner">
        <div class="fx-r fx-bc">
          <div class="label">{{ title }}名称:</div>
          <div style="width: 130px" class="value">
            <el-input
              v-model="formValidate.name"
              :placeholder="title + '名称'"
            />
          </div>

          <div class="value">
            <el-button @click="tapSearch" type="primary" icon="el-icon-zoom-in"
              >搜索</el-button
            >
          </div>
          <div class="fx-g1"></div>
          <Button
            type="primary"
            class="bnt"
            icon="md-add"
            v-if="isAdd"
            @click="tapAdd"
            >添加{{ title }}</Button
          >
        </div>
      </div>
    </Card>

    <Card :bordered="false" dis-hover class="ivu-mt">
      <el-table
        :data="listData"
        :max-height="elMain - 220"
        style="margin-top: 10px"
        border
        row-key="id"
      >
        <el-table-column label="序" align="center" prop="name" width="80px">
          <template #default="scope">
            {{ scope.$index + 1 }}
          </template>
        </el-table-column>

        <el-table-column label="ID" align="center" prop="id" width="80">
        </el-table-column>

        <el-table-column label="单位名称" align="center" prop="title">
        </el-table-column>

        <el-table-column label="联系人" align="center" prop="name">
        </el-table-column>

        <el-table-column label="联系电话" align="center" prop="name">
        </el-table-column>

        <el-table-column label="创建人" align="center" prop="admin_name">
        </el-table-column>

        <el-table-column label="创建时间" align="center" prop="time">
        </el-table-column>

        <el-table-column label="是否显示" align="center" prop="time">
          <template #default="scope">
            <i-switch
              :disabled="true"
              :value="scope.row.status"
              :true-value="1"
              :false-value="0"
              size="large"
            >
              <span slot="open">显示</span>
              <span slot="close">隐藏</span>
            </i-switch>
          </template>
        </el-table-column>

        <el-table-column label="操作" width="250" align="center" fixed="right">
          <template #default="scope">
            <a @click="tapEdit(scope.row.id)">编辑</a>
            <Divider type="vertical" v-if="isAdd" />
            <a @click="tapDel(scope.row.id)" v-if="isAdd">删除</a>
          </template>
        </el-table-column>
      </el-table>

      <div class="acea-row row-right page">
        <Page
          :total="page.count"
          show-elevator
          show-total
          @on-change="tapPage"
          :page-size="page.pageSize"
        />
      </div>
    </Card>

    <!--修改信息-->
    <el-dialog
      :visible.sync="dialogVisible"
      title="单位操作"
      :close-on-click-modal="false"
      width="580px"
    >
      <el-form :model="form" ref="form" label-width="100px">
        <el-row :space="24">
          <el-col :span="24">
            <el-form-item
              label="单位名称"
              prop="title"
              :rules="[{ required: true, message: '请输入单位名称' }]"
            >
              <div class="fx-r">
                <el-input
                  type="text"
                  placeholder="请输入单位名称"
                  v-model="form.title"
                ></el-input>
              </div>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :space="24">
          <el-col :span="12">
            <el-form-item label="联系人">
              <div class="fx-r">
                <el-input
                  type="text"
                  placeholder="请输入单位名称"
                  v-model="form.name"
                ></el-input>
              </div>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="联系电话">
              <div class="fx-r">
                <el-input
                  type="text"
                  placeholder="请输入单位联系电话"
                  v-model="form.tel"
                ></el-input>
              </div>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :space="24">
          <el-col :span="24">
            <el-form-item label="负责电话">
              <div class="fx-r">
                <el-input
                  type="text"
                  placeholder="请输入负责人手机号码，短信通知使用"
                  v-model="form.mobile"
                ></el-input>
              </div>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :space="24">
          <el-col :span="24">
            <el-form-item label="单位描述" prop="tel">
              <div class="fx-r">
                <el-input
                  rows="5"
                  type="textarea"
                  placeholder="单位描述"
                  v-model="form.mono"
                ></el-input>
              </div>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :space="24">
          <el-col :span="24">
            <el-form-item label="显示:" style="margin-bottom: 0">
              <el-radio-group v-model="form.status">
                <el-radio :label="1">是</el-radio>
                <el-radio :label="0">否</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <div slot="footer" class="dialog-footer fx-r fx-ae">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="subAdmin">确 定</el-button>
      </div>
    </el-dialog>

    <bumen_info_member ref="member_info"></bumen_info_member>
  </div>
</template>

<script>
import IPageHeader from '@/layouts/system/page-header/index'
import { mapState, mapActions } from 'vuex'
import UiUpload from '@/ui/upload/index'
import Setting from '@/setting'
import util from '../../../../libs/util'
import {
  SysteBumenDel,
  SysteBumenInfo,
  SysteBumenList,
  SysteBumenSave,
} from '../../../../api/system/bumen'
import Bumen_info_member from './member'
export default {
  name: 'bumen_info_index',
  components: { Bumen_info_member, UiUpload, IPageHeader },
  computed: {
    ...mapState('pub/layout', ['elMain']),
  },
  props: {
    title: {
      type: String,
      default: '',
    },

    typeCode: {
      type: Number,
      default: 0,
    },
    isAdd: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      upUrl: {}, //上传
      upHeaders: {},

      formValidate: {},
      listData: [],
      page: {
        count: 0, //一共多少数量
        pageSize: 0, //每一页码数量
        page: 1, //当前页码
      },

      dialogVisible: false,
      form: {
        img: '', //门头
        status: 1,
      },
    }
  },
  mounted() {
    this.initView()
  },
  methods: {
    initView: function () {
      var upHeaders = {}
      this.upUrl = Setting.apiBaseURL + '/systemv1/upload/index'
      const token = util.cookies.get('system_token')
      if (token) {
        upHeaders['SYSTEM-ACC-TOKEN'] = token
      }
      this.upHeaders = upHeaders
      this.getData()
    },

    tapUser: function (id) {
      this.$refs['member_info'].open(id)
    },

    tapEdit: function (id) {
      this.$utils.loading(this, '获取数据中...')
      SysteBumenInfo({
        id: id,
      })
        .then((res) => {
          this.$utils.loadingClose()
          if (res.code == 200) {
            this.form = res.data
            this.dialogVisible = true
          } else {
            this.$alert(res.msg)
          }
        })
        .catch((err) => {
          console.log(err)
          this.$utils.loadingClose()
          this.$alert('网络繁忙，加载失败，请稍等片刻在尝试!', '系统提示')
        })
    },
    tapDel(id) {
      this.$confirm('此操作将永久删除该单位信息, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        this.$utils.loading(this, '删除数据中...')
        SysteBumenDel({ id: id })
          .then((res) => {
            this.$utils.loadingClose()
            if (res.code == 200) {
              this.getData()
            } else {
              this.$alert(res.msg)
            }
          })
          .catch((err) => {
            this.$utils.loadingClose()
            this.$alert('网络繁忙，加载失败，请稍等片刻在尝试!', '系统提示')
          })
      })
    },

    /**
     * 获取数据
     */
    getData: function () {
      this.$utils.loading(this, '获取数据中...')
      SysteBumenList({
        ...this.formValidate,
        type: this.typeCode,
        page: this.page.page,
      })
        .then((res) => {
          this.$utils.loadingClose()
          if (res.code == 200) {
            this.listData = res.data.data
            this.page.pageSize = res.data.pageSize
            this.page.count = res.data.pageCount
          } else {
            this.$alert(res.msg)
          }
        })
        .catch((err) => {
          console.log(err)
          this.$utils.loadingClose()
          this.$alert('网络繁忙，加载失败，请稍等片刻在尝试!', '系统提示')
        })
    },

    /**
     * 添加单位信息
     */
    tapAdd: function () {
      this.dialogVisible = true
      this.$refs['form'].resetFields()
      this.form = this.$utils.resetFields(this.form, { status: 1 })
    },

    tapSearch: function () {
      this.page.page = 1
      this.getData()
    },

    tapPage: function (page) {
      this.page.page = page
      this.getData()
    },
    /**
     * 提交数据修改
     */
    subAdmin: function () {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.$utils.loading(this, '提交数据中...')
          SysteBumenSave({
            ...this.form,
            type: this.typeCode,
          })
            .then((res) => {
              this.$utils.loadingClose()
              if (res.code == 200) {
                this.$message({
                  message: res.msg,
                  type: 'success',
                })
                this.getData()
                this.dialogVisible = false
              } else {
                this.$alert(res.msg)
              }
            })
            .catch((err) => {
              this.$utils.loadingClose()
              this.$alert('网络繁忙，加载失败，请稍等片刻在尝试!', '系统提示')
            })
        }
      })
    },

    /**
     * 上传头像
     * @param res
     */
    handleAvatarSuccess: function (res) {
      res.code == -1
        ? this.$Notice.error({ title: '系统提示', content: res.msg })
        : (this.form.img = res.data.img)
    },
    /**
     * 清理头像
     */
    handleAvatarDelete: function () {
      this.form.img = ''
    },
  },
}
</script>

<style scoped></style>
